<template>
  <div class="news-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <nav class="nav-container">
        <div class="nav-left">
          <a href="#" class="nav-item" @click="goHome">主页大厅</a>
          <a href="#" class="nav-item active">新闻资讯</a>
        </div>
        <div class="nav-right">
          <button class="user-btn" @click="handleProfileClick">
            <span class="user-icon">👤</span>我的
          </button>
        </div>
      </nav>
    </header>

    <!-- 主要内容区域 -->
    <main class="main-content">
      <div class="container">
        <div class="page-header">
          <h1 class="page-title">新闻资讯</h1>
          <p class="page-subtitle">了解最新校园动态和竞赛资讯</p>
        </div>

        <!-- 新闻列表 -->
        <div class="news-list" v-if="newsList.length > 0">
          <div 
            v-for="news in newsList" 
            :key="news.id" 
            class="news-card"
            @click="handleNewsClick(news.id)"
          >
            <div class="news-header">
              <h3 class="news-title">{{ news.title }}</h3>
              <span class="news-status" :class="getStatusClass(news.status)">
                {{ getStatusText(news.status) }}
              </span>
            </div>
            <div class="news-content">
              <p class="news-description">{{ news.content || news.summary }}</p>
              <div class="news-meta">
                <span class="news-date">📅 {{ formatDate(news.createTime) }}</span>
                <span class="news-author" v-if="news.author">✍️ {{ news.author }}</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 暂无数据 -->
        <div class="no-data" v-else-if="!loading">
          <div class="no-data-icon">📰</div>
          <h3>暂无新闻资讯</h3>
          <p>目前没有发布的新闻，请稍后再来查看</p>
        </div>

        <!-- 加载中 -->
        <div class="loading" v-if="loading">
          <div class="loading-spinner"></div>
          <p>加载中...</p>
        </div>
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useTokenStore } from '@/stores/token.js'
import { getAllNewsService } from '@/api/home.js'

const router = useRouter()
const tokenStore = useTokenStore()

// 数据状态
const newsList = ref([])
const loading = ref(true)

// 加载新闻数据
const loadNewsList = async () => {
  try {
    loading.value = true
    console.log('开始加载新闻数据...')
    const result = await getAllNewsService()
    console.log('新闻API响应:', result)
    
    if (result.code === 200 && result.data) {
      newsList.value = Array.isArray(result.data) ? result.data : result.data.list || []
      console.log('新闻数据加载成功:', newsList.value)
    } else {
      console.log('新闻API返回失败')
      newsList.value = []
    }
  } catch (error) {
    console.error('加载新闻数据失败:', error)
    newsList.value = []
  } finally {
    loading.value = false
  }
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '时间未知'
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  })
}

// 获取状态文本
const getStatusText = (status) => {
  switch (status) {
    case 0:
      return '草稿'
    case 1:
      return '已发布'
    case 2:
      return '已下线'
    default:
      return '未知'
  }
}

// 获取状态样式类
const getStatusClass = (status) => {
  switch (status) {
    case 0:
      return 'status-draft'
    case 1:
      return 'status-published'
    case 2:
      return 'status-offline'
    default:
      return 'status-unknown'
  }
}

// 点击新闻项
const handleNewsClick = (newsId) => {
  console.log('点击新闻:', newsId)
  router.push(`/news-detail/${newsId}`)
}

// 页面挂载时加载数据
onMounted(async () => {
  // 检查登录状态
  if (!tokenStore.token) {
    router.push('/login')
    return
  }
  
  await loadNewsList()
})

// 返回主页
const goHome = () => {
  router.push('/home')
}

// 处理个人中心点击
const handleProfileClick = () => {
  router.push('/profile')
}
</script>

<style scoped>
.news-container {
  min-height: 100vh;
  background: #f5f5f5;
}

/* 顶部导航栏 */
.header {
  background: linear-gradient(135deg, #6278db 0%, #635da6 100%);
  padding: 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
}

.nav-left {
  display: flex;
  gap: 30px;
}

.nav-item {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  font-size: 16px;
  font-weight: 500;
  padding: 8px 16px;
  border-radius: 20px;
  transition: all 0.3s ease;
  cursor: pointer;
}

.nav-item:hover,
.nav-item.active {
  color: white;
  background: rgba(255, 255, 255, 0.2);
}

.nav-right {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-btn {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 8px 20px;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.user-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.user-icon {
  font-size: 16px;
  opacity: 0.9;
}

/* 主要内容区域 */
.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

.container {
  width: 100%;
}

.page-header {
  text-align: center;
  margin-bottom: 40px;
}

.page-title {
  font-size: 36px;
  font-weight: bold;
  color: #333;
  margin: 0 0 10px 0;
}

.page-subtitle {
  font-size: 18px;
  color: #666;
  margin: 0;
}

/* 新闻列表 */
.news-list {
  display: grid;
  gap: 20px;
}

.news-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
}

.news-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
}

.news-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 15px;
}

.news-title {
  font-size: 20px;
  font-weight: bold;
  color: #333;
  margin: 0;
  flex: 1;
  line-height: 1.4;
}

.news-status {
  padding: 4px 12px;
  border-radius: 16px;
  font-size: 12px;
  font-weight: 500;
  margin-left: 15px;
}

.status-draft {
  background: #f0f0f0;
  color: #666;
}

.status-published {
  background: #e8f5e8;
  color: #28a745;
}

.status-offline {
  background: #f8d7da;
  color: #721c24;
}

.status-unknown {
  background: #f8f9fa;
  color: #6c757d;
}

.news-content {
  margin-top: 15px;
}

.news-description {
  color: #666;
  line-height: 1.6;
  margin: 0 0 15px 0;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.news-meta {
  display: flex;
  gap: 20px;
  font-size: 14px;
  color: #888;
}

.news-date,
.news-author {
  display: flex;
  align-items: center;
  gap: 5px;
}

/* 暂无数据 */
.no-data {
  text-align: center;
  padding: 80px 20px;
  color: #999;
}

.no-data-icon {
  font-size: 64px;
  margin-bottom: 20px;
}

.no-data h3 {
  font-size: 24px;
  color: #666;
  margin: 0 0 10px 0;
}

.no-data p {
  font-size: 16px;
  margin: 0;
}

/* 加载中 */
.loading {
  text-align: center;
  padding: 80px 20px;
  color: #999;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #6278db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 20px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-container {
    padding: 10px 15px;
  }
  
  .nav-left {
    gap: 15px;
  }
  
  .nav-item {
    font-size: 14px;
    padding: 6px 12px;
  }
  
  .main-content {
    padding: 20px 15px;
  }
  
  .page-title {
    font-size: 28px;
  }
  
  .page-subtitle {
    font-size: 16px;
  }
  
  .news-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  
  .news-status {
    margin-left: 0;
  }
}
</style>
